package com.xm.servlet.admin;

import com.alibaba.fastjson.JSONObject;
import com.xm.biz.UserAddressBiz;
import com.xm.biz.UserScoreBiz;
import com.xm.biz.UsersBiz;
import com.xm.biz.impl.UserAddressBizImpl;
import com.xm.biz.impl.UserScoreBizImpl;
import com.xm.biz.impl.UsersBizImpl;
import com.xm.dao.UserScoreDao;
import com.xm.po.UserAddress;
import com.xm.po.UserScore;
import com.xm.po.Users;
import com.xm.util.JsonUtils;
import org.apache.commons.beanutils.BeanUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
* @Description: 条件用户查询
* @Author: Mr.Wang
* @Date: 2020/8/15
*/
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    //初始化数据源
    UsersBiz usersBiz = new UsersBizImpl();
    UserScoreBiz userScoreBiz = new UserScoreBizImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        //根据用户编号查询数据(修改)
        if (type.equals("queryOne")){
            queryOne(request,response);
        }
        /*根据名字或编号查询用户*/
        else if (type.equals("queryByNameIdUser")){
            queryByNameIdUser(request,response);
        }
        /*修改用户资料*/
        else if (type.equals("updateUser")){
            try {
                updateUser(request,response);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        /*冻结用户*/
        else if (type.equals("freeze")){
            freeze(request,response);
        }
        /*解冻用户*/
        /*else if (type.equals("thaw")){
            thaw(request,response);
        }*/
        /*查询一个用户(按钮)*/
        else if (type.equals("queryAll")){
            queryAll(request,response);
        }
        /*用户积分查询(所有)*/
        else if (type.equals("scoreQueryAll")){
            scoreQueryAll(request,response);
        }
        /*根据编号删除用户积分信息*/
        else if (type.equals("scoreIdDelete")){
            scoreIdDelete(request,response);
        }
        /*根据编号查询用户积分信息(修改)*/
        else if (type.equals("scoreIdSelect")){
            scoreIdSelect(request,response);
        }
        /*根据编号修改用户积分信息(提交按钮)*/
        else if (type.equals("scoreIdUpdate")){
            try {
                scoreIdUpdate(request,response);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        /*添加用户信息*/
        else if (type.equals("addUser")){
            try {
                addUser(request,response);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    //根据名字或编号查询用户
    public void queryByNameIdUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //String path = request.getContextPath();
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        //调用业务层的方法
        if (id.equals("")&&name.equals("")){
            //无条件搜索
            List<Users> users = usersBiz.userQueryAll();
            request.setAttribute("userList",users);
            request.getRequestDispatcher("admin/user.jsp").forward(request,response);
        }else {
            //有条件搜索
            //根据id条件查询
            if (!id.equals("")&&name.equals("")){
                Users users = usersBiz.queryByOne(Integer.valueOf(id));
                List<Users> list = new ArrayList<>();
                list.add(users);
                request.setAttribute("userList",list);
                request.setAttribute("valueId",id);
                request.getRequestDispatcher("admin/user.jsp").forward(request,response);
            }
            //根据name条件查询
            else if (id.equals("")&&!name.equals("")){
                Users users = usersBiz.queryOne(name);
                List<Users> list = new ArrayList<>();
                list.add(users);
                request.setAttribute("userList",list);
                request.setAttribute("valueName",name);
                request.getRequestDispatcher("admin/user.jsp").forward(request,response);
            }else {
                List<Users> userByName = usersBiz.getUserByName(name, id);
                request.setAttribute("userList",userByName);
                //给文本框赋值
                request.setAttribute("valueId",id);
                //(未完成)
                request.setAttribute("valueName",name);

                request.getRequestDispatcher("admin/user.jsp").forward(request,response);
            }
        }
    }
    //根据用户编号查询数据(修改)
    public void queryOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userId = request.getParameter("userId");
        Users users = usersBiz.queryByOne(Integer.valueOf(userId));

        PrintWriter out = response.getWriter();
        out.print(JsonUtils.objectToJson(users));
        out.flush();
        out.close();
    }
    //修改用户资料
    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Users users = new Users();
        BeanUtils.populate(users,request.getParameterMap());
        //调用业务修改
        usersBiz.update(users);
        //将修改后的数据重新查询
        request.setAttribute("userList",usersBiz.userQueryAll());
        request.getRequestDispatcher("admin/user.jsp").forward(request,response);
    }
    //冻结和解冻用户(要提升)
    public void freeze(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        String userId = request.getParameter("userId");
        String userStatus = request.getParameter("userStatus");
        if (userStatus.equals("解冻该账户")){
            //冻结方法
            boolean b = usersBiz.updateById(Integer.valueOf(userId));
            out.print(b);
            out.flush();
            out.close();
        }else if (userStatus.equals("冻结该账户")){
            //解冻方法
            boolean b = usersBiz.updateByid(Integer.valueOf(userId));
            out.print(b);
            out.flush();
            out.close();
        }

    }
    //解冻用户
    /*public void thaw(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        String userId = request.getParameter("userId");

    }*/
    //查询所有用户(按钮)
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Users users = usersBiz.queryByOne(Integer.valueOf(id));
        PrintWriter out = response.getWriter();
        out.print(JsonUtils.objectToJson(users));
        out.flush();
        out.close();
    }
    //用户积分查询(所有)
    public void scoreQueryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UserScore> userScores = userScoreBiz.queryAll();
        request.setAttribute("scoreVoList",userScores);
        request.getRequestDispatcher("admin/userScore.jsp").forward(request,response);
    }
    //根据编号删除用户积分信息
    public void scoreIdDelete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String scoreId = request.getParameter("scoreId");
        boolean delete = userScoreBiz.delete(Integer.valueOf(scoreId));

        PrintWriter out = response.getWriter();
        out.print(delete);
        out.flush();
        out.close();
    }
    //根据编号查询用户积分信息(修改)
    public void scoreIdSelect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String scoreId = request.getParameter("scoreId");
        UserScore select = userScoreBiz.select(Integer.valueOf(scoreId));

        PrintWriter out = response.getWriter();
        out.print(JsonUtils.objectToJson(select));
        out.flush();
        out.close();
    }
    //根据编号修改用户积分信息(提交按钮)(报错)
    public void scoreIdUpdate(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, ServletException, IOException, ParseException {
        UserScore userScore = new UserScore();
        BeanUtils.populate(userScore,request.getParameterMap());
        String createTime = request.getParameter("createTime");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse(createTime);
        userScore.setCreateTime(parse);
        userScoreBiz.updateById(userScore);
        request.setAttribute("scoreVoList",userScoreBiz.queryAll());
        request.getRequestDispatcher("admin/userScore.jsp").forward(request,response);
    }
    //添加用户信息
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        request.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        Users users = new Users();
        BeanUtils.populate(users,request.getParameterMap());
        users.setCreateTime(new Date());
        boolean b = usersBiz.addUsers(users);
        if (b==true){
            PrintWriter out = response.getWriter();
            out.print("<script>location.href='userAll'</script>");
            out.flush();
            out.close();
        }else {
            PrintWriter out = response.getWriter();
            out.print("<script>location.href='userAll'</script>");
            out.flush();
            out.close();
        }
    }
}
